﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
using Ecopsy.DataTier;
using AgencyEntityLib.BaseEntity;
using System.Web.UI.WebControls;
using System.Reflection;
using Ecopsy.Tools;
 

namespace FormControllers
{
   #region Инструмет по работе с командной строкой
   /// <summary>
   /// Инструментарий по работе со строками 
   /// </summary>
   public class CmdStringEnv
   {
       public static Hashtable GetParams(string CmdStr)
       {
           Hashtable r = new Hashtable();
           char[] s = { ",".ToCharArray()[0] };
           string[] arr = CmdStr.Split(s);
           foreach (string item in arr)
           {

               int i = item.IndexOf("=");
               if (-1 == i) continue;
               string value = item.Substring(i + 1);
               string id = item.Substring(0, i);
               r.Add(id, value);
           }
           return r;
       }

       public static string GetParam(string CmdStr, string ParamId)
       {
           Hashtable tbl = GetParams(CmdStr);
           object obj = tbl[ParamId];
           if (null == obj) return string.Empty;
           return obj.ToString();
       }

       public static void UpdateString(string Parametr, string Value, ref string CmdStr)
       {
           Hashtable tbl = GetParams(CmdStr);
           object v = tbl[Parametr];
           if (null == v) return;

           CmdStr = CmdStr.Replace(Parametr.ToString() + "=" + v.ToString(), Parametr + "=" + Value);
       }


   }

    /// <summary>
    /// Инструменты по формированию скриптов на лету
    /// </summary>
    public static class ScriptBuilder
    {
        public static string  GetAlertScript(string message)
        {
            return "alert('" + message + "');";
        }

        public static string GetAlertScript(List<string> source )
        {
            StringBuilder b = new StringBuilder();
            foreach (string s in source)
            {
                b.Append(s + " <br /> ");
            }
            return GetAlertScript(b.ToString());
        }

        

        public static string GetScript(string Script)
        {
            return "<script language ='javascript' type ='text/javascript' >  " + Script + " </script>";
        }
        public static string GetShowModalScript(string sUrl,int Hight, int Width)
        {

            return "CallModalWindow('" + sUrl + "','" + Hight.ToString() + "','" + Width.ToString() + "');";
        }        
        public static string  GetCloseShowModalScript(string ReturnVar)
        {
            return  "<script language ='javascript' type ='text/javascript' >  window.returnValue ='" + ReturnVar + "';  close(); </script>";
            
        }

        /// <summary>
        /// Сформировать скрипт по инициализации карты и ее центровке
        /// </summary>
        /// <param name="map_canvas"></param>
        /// <returns></returns>
        public static string InitMap(string map_canvas)
        { 
            // Карта гугель
        string res= "function  InitMap(){"+
            "var map = new GMap2(document.getElementById('"+map_canvas+"'));" 
            +"map.setUIToDefault(); "+
            "map.setCenter(new GLatLng(45.4400, 45.0000), 5); }";

            return res;
        }
        
    }
   #endregion


   
  
}
